package zero.springdemo.springquartzdemo.controller

import org.quartz.Job
import org.quartz.JobExecutionContext
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import zero.springdemo.springquartzdemo.core.TestTask
import zero.springdemo.springquartzdemo.core.entity.TaskReq
import zero.springdemo.springquartzdemo.core.quartz.QuartzManager
import zero.springdemo.springquartzdemo.dao.TestDao

/**
 * # 计划任务控制器
 */
@RestController
@RequestMapping("/task")
@CrossOrigin("*")
class QuartzTaskController {

    @Autowired
    private lateinit var quartzManager: QuartzManager

    @Autowired
    private lateinit var testTask: TestTask

    @Autowired
    private lateinit var testDao: TestDao

    //列表
    @GetMapping
    fun list() {


    }

    //添加
    @PostMapping
    fun addTask(task: TaskReq): Any {
        val list = testDao.getList()
        list.forEach { println(it) }

        //匿名内部类。。
        //val job = Job { println(task.name + " 执行！！！") }.javaClass
        quartzManager.addJob(task.name, "user-define-task",
                task.name + "user-trigger", "user-define-trigger",
                testTask.javaClass, "0/10 * * * * ?")
        return task;
    }
}